package com.loctek.importexport.mvp.acitvity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loctek.importexport.R;
import com.loctek.importexport.base.BaseActivity;
import com.loctek.importexport.bean.getproductResponse;
import com.loctek.importexport.bean.getstoresiteResponse;
import com.loctek.importexport.bean.intostoresiteResponse;
import com.loctek.importexport.bean.outstoresiteResponse;
import com.loctek.importexport.mvp.adapter.ProductAdapter;
import com.loctek.importexport.mvp.presenter.OutHousingPersenter;
import com.loctek.importexport.mvp.view.OutHousingView;
import com.loctek.importexport.util.ScreenUtil;
import com.loctek.importexport.util.ToastUtils;
import com.loctek.importexport.views.UIConfirmOutHousAlertView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * 出库
 */
public class OutHousingActivity extends BaseActivity<OutHousingView, OutHousingPersenter> implements OutHousingView {


    @BindView(R.id.ed_product_code)
    EditText edProductCode;
    @BindView(R.id.keji_loan_release_order_ll)
    LinearLayout kejiLoanReleaseOrderLl;
    @BindView(R.id.rb_box)
    RadioButton rbBox;
    @BindView(R.id.rb_one)
    RadioButton rbOne;
    @BindView(R.id.iv_product)
    ImageView ivProduct;
    @BindView(R.id.tv_info)
    TextView tvInfo;
    @BindView(R.id.llyt_product_info)
    LinearLayout llytProductInfo;
    @BindView(R.id.rg_box)
    RadioGroup rgBox;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.rlyt_num_layout)
    RelativeLayout rlytNumLayout;
    @BindView(R.id.ed_num)
    EditText edNum;
    @BindView(R.id.iv_search_clear)
    ImageView ivSearchClear;
    @BindView(R.id.ed_storesite)
    EditText edStoresite;
    @BindView(R.id.iv_search_clear1)
    ImageView ivSearchClear1;
    @BindView(R.id.empty_ll)
    LinearLayout emptyLl;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    ProductAdapter adapter;
    List<getstoresiteResponse.ProductInfo> list = new ArrayList<>();
    @BindView(R.id.item_ll)
    LinearLayout itemLl;
    getstoresiteResponse data;
    getproductResponse data0;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_out_housing;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        setTitle(getResources().getString(R.string.outgoing));


        adapter = new ProductAdapter(OutHousingActivity.this, list);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(OutHousingActivity.this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);

        edProductCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString())) {
                    ivSearchClear.setVisibility(View.VISIBLE);
                    getPresenter().getproduct(editable.toString());
                } else {
                    llytProductInfo.setVisibility(View.GONE);
                    rlytNumLayout.setVisibility(View.GONE);
                    ivSearchClear.setVisibility(View.GONE);
                }
            }
        });

        ivSearchClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edProductCode.setText("");
            }
        });

        edStoresite.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString())) {
                    ivSearchClear1.setVisibility(View.VISIBLE);
                    emptyLl.setVisibility(View.GONE);
                    itemLl.setVisibility(View.GONE);
                    list.clear();
                    adapter.notifyDataSetChanged();
                    getPresenter().getstoresite(editable.toString());
                } else {
                    ivSearchClear1.setVisibility(View.GONE);
                    emptyLl.setVisibility(View.GONE);
                    itemLl.setVisibility(View.GONE);
                    list.clear();
                    adapter.notifyDataSetChanged();
                }
            }
        });

        ivSearchClear1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edStoresite.setText("");
            }
        });


        edStoresite.setFocusable(true);
        edStoresite.setFocusableInTouchMode(true);
        edStoresite.requestFocus();
        kejiLoanReleaseOrderLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (CheckParam()) {
                    int sum;
                    int num = 0;
                    if (!TextUtils.isEmpty(edNum.getText().toString().trim())) {
                        num = Integer.parseInt(edNum.getText().toString().trim());
                    }
                    if (rgBox.getCheckedRadioButtonId() == R.id.rb_box) {
                        sum = num * data0.data.pcs;
                    } else {
                        sum = num;
                    }

                    final UIConfirmOutHousAlertView alertView = new UIConfirmOutHousAlertView(OutHousingActivity.this, data0, OutHousingActivity.this.data, sum, edStoresite.getText().toString());
                    final int number = sum;
                    alertView.setClicklistener(new UIConfirmOutHousAlertView.ClickListenerInterface() {
                        @Override
                        public void doLeft() {
                            alertView.dismiss();
                        }

                        @Override
                        public void doRight() {
                            getPresenter().outstoresite(edStoresite.getText().toString(), edProductCode.getText().toString(), number);
                            alertView.dismiss();
                        }
                    });
                    alertView.show();
                }
            }
        });

    }

    private boolean CheckParam() {
        if (TextUtils.isEmpty(edProductCode.getText().toString())) {
            ToastUtils.showToast(getResources().getString(R.string.sweep_product_code));
            return false;
        }

        if (TextUtils.isEmpty(edNum.getText().toString())) {
            ToastUtils.showToast(getResources().getString(R.string.enter_number));
            return false;
        }

        if (TextUtils.isEmpty(edStoresite.getText().toString())) {
            ToastUtils.showToast(getResources().getString(R.string.sweep_library_code));
            return false;
        }

        if (data0 == null) {
            ToastUtils.showToast(getResources().getString(R.string.product_not_exist));
            return false;
        }

        if (data == null) {
            ToastUtils.showToast(getResources().getString(R.string.library_not_exist));
            return false;
        }
        return true;
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }


    @NonNull
    @Override
    public OutHousingPersenter createPresenter() {
        return new OutHousingPersenter(getApp());
    }

    @Override
    public void getproduct(final getproductResponse data) {
        data0 = data;
        llytProductInfo.setVisibility(View.VISIBLE);
        rlytNumLayout.setVisibility(View.VISIBLE);
        LoadingImg(ivProduct, ScreenUtil.dip2px(60), ScreenUtil.dip2px(60), data.data.imageurl);

        ivProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(OutHousingActivity.this, SeeBigImgActivity.class);
                intent.putExtra("url", data.data.imageurl);
                startActivity(intent);
            }
        });

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("name:" + data.data.name + "\t  barcode:" + data.data.barcode + "\n");
        stringBuffer.append("hqnum:" + data.data.hqnum + "\t  hscode:" + data.data.hscode + "\n");
        stringBuffer.append("width:" + data.data.width + "\t  height:" + data.data.height + "\n");
        stringBuffer.append("weight:" + data.data.weight + "\t  length:" + data.data.length + "\n");
        stringBuffer.append("pcs:" + data.data.pcs + " \t  traynum:" + data.data.traynum + "\n");
        tvInfo.setText(stringBuffer);

        edNum.setFocusable(true);
        edNum.setFocusableInTouchMode(true);
        edNum.requestFocus();

        edNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString())) {
                    if (rgBox.getCheckedRadioButtonId() == R.id.rb_box) {
                        int num = 0;
                        if (!TextUtils.isEmpty(edNum.getText().toString().trim())) {
                            num = Integer.parseInt(edNum.getText().toString().trim());
                        }
                        int sum = num * data.data.pcs;
                        tvNum.setText("* " + data.data.pcs + " pcs = " + sum + "pcs");
                    } else {
                        tvNum.setText("pcs");
                    }
                }
            }
        });

        if (rgBox.getCheckedRadioButtonId() == R.id.rb_box) {
            int num = 0;
            if (!TextUtils.isEmpty(edNum.getText().toString().trim())) {
                num = Integer.parseInt(edNum.getText().toString().trim());
            }
            int sum = num * data.data.pcs;
            tvNum.setText("* " + data.data.pcs + " pcs = " + sum + "pcs");
        } else {
            tvNum.setText("pcs");
        }


        rgBox.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (llytProductInfo.getVisibility() == View.VISIBLE) {
                    if (i == R.id.rb_box) {
                        int num = 0;
                        if (!TextUtils.isEmpty(edNum.getText().toString().trim())) {
                            num = Integer.parseInt(edNum.getText().toString().trim());
                        }
                        int sum = num * data.data.pcs;
                        tvNum.setText("* " + data.data.pcs + " pcs = " + sum + "pcs");
                    } else {
                        tvNum.setText("pcs");
                    }
                }
            }
        });


    }

    @Override
    public void getstoresite(getstoresiteResponse data) {
        edProductCode.setFocusable(true);
        edProductCode.setFocusableInTouchMode(true);
        edProductCode.requestFocus();

        this.data = data;
        if (data.data.productList == null || data.data.productList.size() == 0) {
            emptyLl.setVisibility(View.VISIBLE);
            itemLl.setVisibility(View.GONE);
        } else {
            emptyLl.setVisibility(View.GONE);
            itemLl.setVisibility(View.VISIBLE);
            list.addAll(data.data.productList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void outstoresite(outstoresiteResponse data) {
        ToastUtils.showToast(getResources().getString(R.string.outgoing_success));
        list.clear();
        adapter.notifyDataSetChanged();
        getPresenter().getstoresite(edStoresite.getText().toString());
    }

}
